package cn.com.jsepc.rep.appframe;

import java.util.ArrayList;

import javax.servlet.ServletException;

import cn.com.jsepc.appframe.system.AppframeOrgClient;
import cn.com.jsepc.appframe.system.domain.BaseOrg;
import cn.com.jsepc.appframe.system.service.AppframeOrg;

import com.jarp.db.DAO;

public class AppUtilTest {
	AppframeOrg appframeorg=null;
	
	public void syncDept(long orgpid){
		appframeorg=AppframeOrgClient.getInstance();
		ArrayList sqls=new ArrayList();
		//获取根，省公司
		BaseOrg[] root=appframeorg.getChildrenBaseOrgs(orgpid);
		if(root!=null&&root.length>0){
//			System.out.println("***start");
			for (int k=0;k<root.length;k++){
//				System.out.println("["+k+"]***start");
				sqls.add("insert into tb_corp_dept(ID,NAME,PID,CODE) values('"+root[k].getId()+"','"+root[k].getName()+"','"+orgpid+"','"+root[k].getCode()+"')");
				System.out.println("root["+k+"].getId():"+root[k].getId()+" name :"+root[k].getName());
//				System.out.println("["+k+"]***end");
				
				syncDept(root[k].getId());
			}
//			System.out.println("***start");
		}
		
	}
	
	public void syncDep(){
		appframeorg=AppframeOrgClient.getInstance();
		//获取根，省公司
		BaseOrg[] root=appframeorg.getChildrenBaseOrgs(null);
		ArrayList sqls=new ArrayList();
		sqls.add("delete from tb_corp_dept where 1=1");
		if(root!=null&&root.length>0){
//			for (int k=0;k<root.length;k++){
//				System.out.println("root[k].getId():"+root[k].getId());
//			}
			
			sqls.add("insert into tb_corp_dept(ID,NAME,PID,CODE) values('"+root[0].getId()+"','"+root[0].getName()+"','-1','"+root[0].getId()+"')");
//			//获取省公司部门根
			BaseOrg[] rootDep=appframeorg.getChildrenBaseOrgsOfDept(root[0].getId());
			
			for(int j=0;j<rootDep.length;j++){
				sqls.add("insert into tb_corp_dept(ID,NAME,PID,CODE) values('"+rootDep[j].getId()+"','"+rootDep[j].getName()+"','"+root[0].getId()+"','"+rootDep[j].getId()+"')");
				//获取省公司部门
				BaseOrg[] children=appframeorg.getChildrenBaseOrgsOfDept(rootDep[j].getId());
				for(int i=0;i<children.length;i++){
					String sql="insert into tb_corp_dept(ID,NAME,PID,CODE) values('"+children[i].getId()+"','"+children[i].getName()+"','"+rootDep[j].getId()+"','"+children[i].getId()+"')";
					sqls.add(sql);
				}
			}
			
		}
		DAO dao=new DAO();
		try {
			dao.execCommand("Jarp", sqls);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main(String [] args){
		AppUtilTest aut=new AppUtilTest();
//		aut.syncDept(101);
	}
}
